package com.meirengu.channel.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.meirengu.channel.service.ChangYuanService;
import com.meirengu.utils.HttpUtil;
import com.meirengu.utils.MD5Util;
import com.meirengu.webview.common.Constants;
import com.meirengu.webview.model.KwCyphPush;
import com.meirengu.webview.model.KwCyphPushLog;
import com.meirengu.webview.service.KwCyphPushLogService;
import com.meirengu.webview.service.KwCyphPushService;
import com.meirengu.webview.utils.ConfigUtil;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 畅愿
 *
 * @author Mao
 * @create 2017-12-15 12:11
 */
@Service
public class ChangYuanServiceImpl implements ChangYuanService {

    Logger logger = LoggerFactory.getLogger(ChangYuanServiceImpl.class);
    @Autowired
    private KwCyphPushService kwCyphPushService;

    @Autowired
    private KwCyphPushLogService kwCyphPushLogService;

    /**
     * @param
     * @return
     */

    public Integer applyLoan(String name, String mobile, String ip, String receivedChannel,
                             String sex, String birth, String city, String income, String loanAmount,
                             String age, String hasCreditCard, String hasHouse, String hasHousingLoan,
                             String hasCar, String hasCarLoan, String hasLifeInsurance, String insurancePay,
                             String welfare, String houseFund, String applyTime) throws Exception {

        String url = ConfigUtil.getConfig("changyuan.puhui.url");
        String channelNo = ConfigUtil.getConfig("changyuan.puhui.channelno");
        String key = ConfigUtil.getConfig("changyuan.puhui.key");


        JSONObject jsonObject = new JSONObject();
        jsonObject.put("channelNo", channelNo);
        jsonObject.put("name", name);
        jsonObject.put("mobile", mobile);
        jsonObject.put("ip", ip);
        jsonObject.put("receivedChannel", receivedChannel);
        /*params.put("idCard", idCard);*/
        jsonObject.put("sex", sex);
        jsonObject.put("birth", birth);
        jsonObject.put("city", city);
        jsonObject.put("income", income);
        jsonObject.put("loanAmount", loanAmount);
        /*params.put("entryDate", entryDate);*/
        jsonObject.put("age", age);
        jsonObject.put("hasCreditCard", hasCreditCard);
        jsonObject.put("hasHouse", hasHouse);
        jsonObject.put("hasHousingLoan", hasHousingLoan);
        jsonObject.put("hasCar", hasCar);
        jsonObject.put("hasCarLoan", hasCarLoan);
        jsonObject.put("hasLifeInsurance", hasLifeInsurance);
        jsonObject.put("insurancePay", insurancePay);
        jsonObject.put("welfare", welfare);
        jsonObject.put("houseFund", houseFund);
        jsonObject.put("applyTime", applyTime);
        jsonObject.put("sign", MD5Util.get32MD5(channelNo + name + mobile + key + applyTime));


        //接口调用信息表
        KwCyphPush kwCyphPush = new KwCyphPush();
        kwCyphPush.setInterfaceCode("cyph");
        kwCyphPush.setInterfaceName("畅愿普惠");
        kwCyphPush.setMobile(mobile);
        kwCyphPush.setCode(Constants.BUSINESS_STATUS_FAIL);


        KwCyphPushLog kwCyphPushLog = new KwCyphPushLog();
        kwCyphPushLog.setInterfaceCode("cyph");
        kwCyphPushLog.setInterfaceName("畅愿普惠");
        kwCyphPushLog.setMobile(mobile);
        kwCyphPushLog.setSendUrl(url.toString());
        kwCyphPushLog.setParams(jsonObject.toString());


        HttpUtil.HttpResult hr = null;
        try {
            hr = HttpUtil.doPost(url, jsonObject.toString());
            int code = hr.getStatusCode();
            if (code == 200) {
                kwCyphPushLog.setBackUrl(hr.toString());
                kwCyphPushLog.setHttpCode(200);
                kwCyphPushLog.setHttpContent(hr.toString());

                logger.info("==ChangYuanServiceImpl.applyLoan request success. result:{}", hr.toString());
                String content = hr.getContent();
                JSONObject returnMsg = JSONObject.parseObject(content);
                String rCode = returnMsg.getString("err");
                String rMsg = returnMsg.getString("msg");
                if ("0".equals(rCode)) {
                    kwCyphPush.setCode(Constants.BUSINESS_STATUS_SUCCESS);
                    kwCyphPush.setMessage("成功");
                    logger.info("==ChangYuanServiceImpl.applyLoan business success");
                } else if ("1".equals(rCode)) {
                    if ("手机号存在".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_REPEAT);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                        logger.warn("==ChangYuanServiceImpl.applyLoan business fail");
                    } else {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                        logger.warn("==ChangYuanServiceImpl.applyLoan business fail");
                    }
                } else {
                    kwCyphPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                    kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    logger.warn("==ChangYuanServiceImpl.applyLoan business fail");
                }
            } else {
                kwCyphPushLog.setBackUrl(hr.toString());
                kwCyphPushLog.setHttpCode(code);
                kwCyphPushLog.setHttpContent(hr.toString());
                logger.warn("==ChangYuanServiceImpl.applyLoan request fail. code:{}, result:{}", code, hr.toString());
            }
        } catch (Exception e) {
            kwCyphPushLog.setHttpCode(408);
            kwCyphPushLog.setHttpContent("Exception");
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        } finally {
            kwCyphPushService.insert(kwCyphPush);
            kwCyphPushLog.setPushId(kwCyphPush.getId());
            kwCyphPushLogService.insert(kwCyphPushLog);
            return kwCyphPush.getCode();
        }


    }

}
